import api from "../../../../../../common/api.js";
const app = getApp();

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },

  /**
   * 组件的初始数据
   */
  data: {
    visitingCardList: [],
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {
      let t = this
      t.loadCardList()
    },
    detached: function () {

    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击卡片
    onClickCard(e) {
      let id = e.currentTarget.dataset.id || ''
      this.jumpUserStore(id)
    },

    // 跳转个人网络店铺
    jumpUserStore(id) {
      let t = this
      let tmp = t.data.visitingCardList.filter((item) => {
        return item.tmpUniqueID === id
      })
      app.globalData.posterTmp = tmp[0] || {}
      if (t.checkUserInfoIntegrity()) {
        wx.navigateTo({
          url: `/pages/visitors/tools/posterCard/posterCard`
        })
      } else {
        wx.navigateTo({
          url: `/pages/mine/myInformation/myInformation?jump=${encodeURIComponent('/pages/visitors/tools/posterCard/posterCard')}`
        })
      }
    },

    // 获取名片模板列表 
    loadCardList() {
      let t = this
      api.getModelImg({
        modelType: "BUSINESSCARD"
      }, (res) => {
        let list = res.data.content || [];
        let visitingCardList = list.map((item) => {
          return {
            img: item.imgUrl || '',
            tmpID: item.modelCode || '',
            tmpUniqueID: item.code || '',
            bgImg: item.backgroundImgUrl || ''
          }
        })
        t.setData({
          visitingCardList,
        });
      });
    },

    // 检查用户信息是否填写完整
    checkUserInfoIntegrity() {
      let settingInfo = wx.getStorageSync('settingInfo');
      let keys = ['imgArray', 'name', 'tel', 'address', 'storeName']
      let isIntegrity = true
      if (settingInfo) {
        keys.forEach(item => {
          if (!(settingInfo[item] && settingInfo[item] != null)) {
            isIntegrity = false
          }
        })
      } else {
        isIntegrity = false
      }
      return isIntegrity
    }
  }
})